package com.anti.modular.bus.billdepositadjustment.service.impl;

import com.anti.core.context.login.LoginContextHolder;
import com.anti.core.utils.IdFlowingUtil;
import com.anti.modular.bus.billdepositadjustment.param.BillDepositParam;
import com.anti.modular.bus.billdepositadjustment.service.BillDepositAdjustmentService;

import com.anti.modular.bus.housebill.entity.HouseBill;
import com.anti.modular.bus.housebill.service.HouseBillService;
import com.anti.modular.bus.housebilldetail.entity.HouseBillDetail;
import com.anti.modular.bus.housebilldetail.service.HouseBillDetailService;


import com.anti.jdbc.DBUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @program: anti
 * @title:
 * @author:
 * @date: 2021-06-24 18:49
 **/
@Service
public class BillDepositAdjustmentImpl implements BillDepositAdjustmentService {

    @Resource
    HouseBillDetailService houseBillDetailService;
    @Resource
    HouseBillService houseBillService;
    @Resource
    DBUtils dbUtils;

    @Override
    public void add(BillDepositParam billDepositParam) {
        Long leaseId=billDepositParam.getLeaseId();
        BigDecimal deposit=billDepositParam.getDeposit();//房屋押金
        Integer yjStyle=billDepositParam.getYjStyle();
        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
        HouseBill houseBill=new HouseBill();
//        houseBill.setQsnum(qs);
        houseBill.setQs("押金調整");
        houseBill.setBillType(4);//押金账单
        String snowflakesId = IdFlowingUtil.getSnowflakesId("QYZDZJ");
        houseBill.setHouseLeaseId(leaseId);
        houseBill.setNo(snowflakesId);
        if(yjStyle==1){//涨
            houseBill.setSubtotal(billDepositParam.getAmount());
        }else {
            houseBill.setSubtotal(billDepositParam.getAmount().negate());
        }
        houseBill.setBillStatus(0);
        //才有效
        houseBill.setStatus(0);
        houseBill.setCreateTime(new Date());
        houseBill.setCreateUser(sysLoginUserId);
        Long billPk = houseBillService.add(houseBill);
        HouseBillDetail houseBillDetail=new HouseBillDetail();
        if(yjStyle==1){//涨
            houseBillDetail.setBillInOutType(1);
            houseBillDetail.setAmount(billDepositParam.getAmount());
        }else {
            houseBillDetail.setBillInOutType(2);
            houseBillDetail.setAmount(billDepositParam.getAmount().negate());
        }
        houseBillDetail.setRentAmountName("房屋押金");
        houseBillDetail.setDepositHydropowerFlag(1);
        houseBillDetail.setHouseBillId(billPk);
        houseBillDetail.setRentAmountType(0);
        houseBillDetail.setStatus(0);
        houseBillDetail.setCreateTime(new Date());
        houseBillDetail.setCreateUser(sysLoginUserId);
        houseBillDetailService.add(houseBillDetail);
        String sqlYjSel="update bus_lease_deposit set amount=?,update_time=?,update_user=? where lease_id=? and depositid=3 and status=0";
        dbUtils.execSql(sqlYjSel,deposit,new Date(),sysLoginUserId,leaseId);
        String sql="update house_lease set deposit=?,update_time=?,update_user=? where id=? and status=0 and lease_status <> 2";
        dbUtils.execSql(sql,deposit,new Date(),sysLoginUserId,leaseId);
    }
}
